package com.bw.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bw.service.TmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class TmsController {

    //注入依赖
    @Autowired
    TmsService tmsService;


    @RequestMapping("/showkanban")
    public Object showkanban(String nowDate){
        List<Map> maps = tmsService.showkanban(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONArray dataArray = new JSONArray();

        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("dimValue","运输次数");
        JSONArray indiArray1 = new JSONArray();
        JSONObject indiObject1 = new JSONObject();
        indiObject1.put("value",maps.get(0).get("count"));
        indiArray1.add(indiObject1);
        jsonObject1.put("indicators",indiArray1);
       dataArray.add(jsonObject1);

        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("dimValue","运输里程");
        JSONArray indiArray2 = new JSONArray();
        JSONObject indiObject2 = new JSONObject();
        indiObject2.put("value",maps.get(0).get("clicheng"));
        indiArray2.add(indiObject2);
        jsonObject2.put("indicators",indiArray2);
        dataArray.add(jsonObject2);

        JSONObject jsonObject3 = new JSONObject();
        jsonObject3.put("dimValue","运输时长");
        JSONArray indiArray3 = new JSONArray();
        JSONObject indiObject3 = new JSONObject();
        indiObject3.put("value",maps.get(0).get("shichang"));
        indiArray3.add(indiObject3);
        dataArray.add(jsonObject3);
        jsonObject3.put("indicators",indiArray3);
        jsonObject.put("data",dataArray);

        return jsonObject;
    }


    @RequestMapping("/showfanpai")
    public Object showfanpai(String nowDate){
        List<Map> list = tmsService.showfanpai(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        jsonObject.put("data",list.get(0).get("count"));
        return jsonObject;
    }


    @RequestMapping("/showlunbo")
    public Object showlunbo(String nowDate){
        List<Map> maps = tmsService.showlunbo(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");

        JSONObject dataObject = new JSONObject();

        JSONArray coluArray = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("name","机构");
        jsonObject1.put("id","name");
        coluArray.add(jsonObject1);

        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("name","运完里程");
        jsonObject2.put("id","lichneg");
        coluArray.add(jsonObject2);

        JSONObject jsonObject3 = new JSONObject();
        jsonObject3.put("name","运完次数");
        jsonObject3.put("id","cishu");
        coluArray.add(jsonObject3);

        JSONObject jsonObject4 = new JSONObject();
        jsonObject4.put("name","运完时长");
        jsonObject4.put("id","shichang");
        coluArray.add(jsonObject4);

        JSONObject jsonObject5 = new JSONObject();
        jsonObject5.put("name","每次平均时长");
        jsonObject5.put("id","pinsc");
        coluArray.add(jsonObject5);

        JSONObject jsonObject6 = new JSONObject();
        jsonObject6.put("name","每次平均里程");
        jsonObject6.put("id","pinlc");
        coluArray.add(jsonObject6);

        JSONArray rowArray = new JSONArray();
        for (Map map : maps) {
            JSONObject rowObject = new JSONObject();
            rowObject.put("name",map.get("name"));
            rowObject.put("lichneg",map.get("lichneg"));
            rowObject.put("cishu",map.get("cishu"));
            rowObject.put("shichang",map.get("shichang"));
            rowObject.put("pinsc",map.get("pinsc"));
            rowObject.put("pinlc",map.get("pinlc"));
            rowArray.add(rowObject);
        }
        dataObject.put("columns",coluArray);
        dataObject.put("rows",rowArray);

        jsonObject.put("data",dataObject);
        return jsonObject;
    }


    @RequestMapping("/showzhuzhuang")
    public Object showzhuzhuang(String nowDate){
        List<Map> maps = tmsService.showzhuzhuang(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");

        JSONObject dataObject = new JSONObject();

        JSONArray cateArray = new JSONArray();
        for (Map map : maps) {
            cateArray.add(map.get("name"));
        }

        JSONArray serArray = new JSONArray();
        JSONObject serObject = new JSONObject();
        serObject.put("name","成功数");
        JSONArray data2Array = new JSONArray();
        for (Map map : maps) {
            data2Array.add(map.get("count"));
        }
        serObject.put("data",data2Array);
        serArray.add(serObject);

        dataObject.put("categories",cateArray);
        dataObject.put("series",serArray);

        jsonObject.put("data",dataObject);

        return jsonObject;
    }
}
